package repository

import (
	"context"
	"riceguess_backend/internal/domain"
	"riceguess_backend/internal/repository/cache"
)

type AnswerRepository interface {
	Store(ctx context.Context, answer domain.Answer) error
	Get(ctx context.Context, id string) (domain.Answer, error)
}

type CachedAnswerRepository struct {
	cache cache.AnswerCache
}

func NewCachedAnswerRepository(cache cache.AnswerCache) AnswerRepository {
	return &CachedAnswerRepository{cache: cache}
}

func (repo *CachedAnswerRepository) Store(ctx context.Context, answer domain.Answer) error {
	var key = "ai:answer:" + answer.Id
	return repo.cache.Set(ctx, key, cache.Answer{
		Title: answer.Title,
		Truth: answer.Truth,
	})
}

func (repo *CachedAnswerRepository) Get(ctx context.Context, id string) (domain.Answer, error) {
	var key = "ai:answer:" + id
	answer, err := repo.cache.Get(ctx, key)
	if err != nil {
		return domain.Answer{}, err
	}
	return domain.Answer{
		Title: answer.Title,
		Truth: answer.Truth,
	}, nil
}
